﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BUS;
using DTO;
using System.Data;

namespace MasterPageGroupF3.AdminPages
{
    public partial class WebForm4 : System.Web.UI.Page
    {
        private List<PROVIDER> list;
        protected void Page_Load(object sender, EventArgs e)
        {
            list = PROVIDERBUS.Getlist();
            if (!IsPostBack)
            {
                GridProvider.DataSource = list;
                GridProvider.DataBind();
            }
        }
        protected void GridProvider_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridProvider.PageIndex = e.NewPageIndex;
            GridProvider.DataSource = list;
            GridProvider.DataBind();
        }
        private string ConvertSortDirectionToSql(SortDirection sortDirection)
        {
            string newSortDirection = String.Empty;

            switch (sortDirection)
            {
                case SortDirection.Ascending:
                    newSortDirection = "ASC";
                    break;

                case SortDirection.Descending:
                    newSortDirection = "DESC";
                    break;
            }

            return newSortDirection;
        }
        protected void GridProvider_Sorting(object sender, GridViewSortEventArgs e)
        {
            DataTable dataTable = GridProvider.DataSource as DataTable;

            if (dataTable != null)
            {
                DataView dataView = new DataView(dataTable);
                dataView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql(e.SortDirection);

                GridProvider.DataSource = dataView;
                GridProvider.DataBind();
            }
        }
        protected void Add_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("ManageProviders_Add.aspx");
        }

        protected void Edit_Click(object sender, ImageClickEventArgs e)
        {
            int flag = 0;
            for (int i = 0; i < GridProvider.Rows.Count; i++)
            {
                CheckBox Chk = (CheckBox)GridProvider.Rows[i].FindControl("chk");
                if (Chk != null)
                {
                    if (Chk.Checked == true)
                    {
                        int id = Int32.Parse(GridProvider.Rows[i].Cells[1].Text);
                        Session.Add("IDPROVIDER", id);
                        flag = 1;
                        Response.Redirect("ManageProviders_Add.aspx");
                        break;
                    }
                }
            }
            if (flag == 0)
                Response.Write("<script>alert('Bạn phải check vào 1 nhà cung cấp!');</script>");
        }

        protected void Delete_Click(object sender, ImageClickEventArgs e)
        {
            int flag = 0;
            for (int i = 0; i < GridProvider.Rows.Count; i++)
            {
                CheckBox Chk = (CheckBox)GridProvider.Rows[i].FindControl("chk");
                if (Chk != null)
                {
                    if (Chk.Checked == true)
                    {
                        int id = Int32.Parse(GridProvider.Rows[i].Cells[1].Text);
                        PROVIDERBUS.Delete(id);
                        flag = 1;
                    }
                }
            }
            if (flag == 0)
                Response.Write("<script>alert('Bạn phải check vào 1 nhà cung cấp!');</script>");
            else
                Response.Redirect("ManageProviders.aspx");
        }

        protected void Help_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("HelpAdmin.aspx");
        }

        protected void Go_Click(object sender, EventArgs e)
        {
            String txt = txtsearch.Text;
            List<PROVIDER> l = new List<PROVIDER>();
            for (int i = 0; i < list.Count ; i++)
                if (list[i].NAME.Contains(txt) || list[i].PHONE.Contains(txt) || list[i].INTRODUCE_COMPANY.Contains(txt) || list[i].ADDRESS.Contains(txt) || list[i].USERNAME.Contains(txt))
                    l.Add(list[i]);
            if (l != null)
            {
                GridProvider.DataSource = l;
                GridProvider.DataBind();
            }
        }

        protected void btreset_Click(object sender, EventArgs e)
        {
            GridProvider.DataSource = list;
            GridProvider.DataBind();

        }
    }
}